package com.syh.bishe.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.syh.bishe.Util.RedisConstants;
import com.syh.bishe.mapper.CompaniesMapper;
import com.syh.bishe.pojo.Bill;
import com.syh.bishe.mapper.BillMapper;
import com.syh.bishe.pojo.Companies;
import com.syh.bishe.pojo.DataIndex;
import com.syh.bishe.pojo.Transactions;
import com.syh.bishe.service.IBillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author syh
 * @since 2024-07-05
 */
@Service
@RequiredArgsConstructor
public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements IBillService {
    private final BillMapper billMapper;
    private final CompaniesMapper companiesMapper;

    private final StringRedisTemplate redisTemplate;

    @Override
    public void addBill_success(Transactions transactions) {
        Bill bill = new Bill();
        bill.setTransactionId(transactions.getId());
        bill.setBuyerId(transactions.getBuyerId());
        bill.setSellerId(transactions.getSellerId());
        Companies buyer = companiesMapper.getCompany(transactions.getBuyerId());
        Companies seller = companiesMapper.getCompany(transactions.getSellerId());
        bill.setInfo(buyer.getName() + "公司向" + seller.getName() + "公司通过" + transactions.getType() + "" +
                "方式购买" + transactions.getCreditAmount() + "吨碳额度" +
                "付款" + transactions.getFinalSum() + "元");
        bill.setAmount(transactions.getFinalSum());
        bill.setTime(LocalDateTime.now());
        billMapper.insert(bill);
    }

    @Override
    public void addBill_cansel(Transactions transactions) {
        Bill bill = new Bill();
        bill.setTransactionId(transactions.getId());
        bill.setBuyerId(transactions.getBuyerId());
        bill.setSellerId(transactions.getSellerId());
        Companies buyer = companiesMapper.getCompany(transactions.getBuyerId());
        Companies seller = companiesMapper.getCompany(transactions.getSellerId());
        bill.setInfo("订单取消，对"+buyer.getName()+"公司和"+seller.getName()+"公司进行退款，并更新双方碳信用额度");
        bill.setAmount(transactions.getFinalSum());
        bill.setTime(LocalDateTime.now());
        billMapper.insert(bill);
    }

    @Override
    public List<DataIndex> getData(Integer id) {
        String s = redisTemplate.opsForValue().get(RedisConstants.MAIN_IAO_DATA + id);
        if (StrUtil.isNotBlank(s)) {
            return JSONUtil.toList(s, DataIndex.class);
        }
        List<DataIndex> res = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        String formatDateTime = now.format(formatter);
        BigDecimal data = billMapper.getData(id,formatDateTime);
        BigDecimal data2 = billMapper.getData2(id,formatDateTime);
        data=data == null ? BigDecimal.ZERO : data;
        data2=data2 == null ? BigDecimal.ZERO : data2;
        BigDecimal sum=data.add(data2);
        if(sum.compareTo(BigDecimal.ZERO)==0){
            res.add(new DataIndex("本月支出",BigDecimal.ZERO,BigDecimal.ZERO));
            res.add(new DataIndex("本月收入",BigDecimal.ZERO,BigDecimal.ZERO));
        }else {
            if(data.compareTo(BigDecimal.ZERO)==0){
                res.add(new DataIndex("本月支出",BigDecimal.ZERO,BigDecimal.ZERO));
            }else {
                res.add(new DataIndex("本月支出",data,data.divide(sum, 4, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100))));
            }
            if(data2.compareTo(BigDecimal.ZERO)==0){
                res.add(new DataIndex("本月收入",BigDecimal.ZERO,BigDecimal.ZERO));
            }else {
                res.add(new DataIndex("本月收入",data2,data2.divide(sum, 4, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100))));
            }
        }
        redisTemplate.opsForValue().set(RedisConstants.MAIN_IAO_DATA +id, JSONUtil.toJsonStr(res), RedisConstants.CACHE_NULL_TTL, TimeUnit.SECONDS);
        return res;
    }
}
